package org.ligson.login;

import org.apache.http.*;
import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

/***
 * curl 'http://10.170.200.171/crux-auth/login' \
 *   -H 'Content-Type: application/x-www-form-urlencoded' \
 *   --data-raw 'username=310000000999999999&password=90&CApsd=&fiscalYear=2021&mofDiv=319900000' \
 *   --compressed \
 *   --insecure
 *
 *
 *
 *   PHPSESSID=MDQxOWQxNjEtNzUyOS00ODM4LWExMTctNTFmODczYWQzODZk
 *
 *   curl -c 2.txt -IL 'http://10.170.200.171/' \
 *   -H 'Cookie: PHPSESSID=MDQxOWQxNjEtNzUyOS00ODM4LWExMTctNTFmODczYWQzODZk'
 *
 *
 *
 *   curl  -IL 'http://10.170.200.171/crux-uc/api/user/me' \
 *   -H 'Cookie: CRUXSESSIONID1=N2NkN2ZmMTItOWNmYi00YTRjLTlmYWMtMjVkYjJmMjQ0YmU4'
 *
 *   CRUXSESSIONID1	N2NkN2ZmMTItOWNmYi00YTRjLTlmYWMtMjVkYjJmMjQ0YmU4
 */
public class LoginDemo {
    public static void main(String[] args) throws Exception {
        BasicCookieStore cookieStore = new BasicCookieStore();
        //DefaultRedirectStrategy strategy = new DefaultRedirectStrategy();

        CloseableHttpClient client = HttpClientBuilder.create()
                //.setRedirectStrategy(redirectStrategy)
                //.setRedirectStrategy(redirectStrategy)
                .setDefaultCookieStore(cookieStore).build();

        List<NameValuePair> loginFormPairs = new ArrayList<>();
        //username=310000000999999999&password=90&CApsd=&fiscalYear=2021&mofDiv=319900000
        loginFormPairs.add(new BasicNameValuePair("username", "310000000999999999"));
        loginFormPairs.add(new BasicNameValuePair("password", "90"));

        UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(loginFormPairs);

        HttpPost loginPost = new HttpPost("http://10.170.200.171/crux-auth/login");
        loginPost.setEntity(encodedFormEntity);
        CloseableHttpResponse res1 = client.execute(loginPost);
        System.out.println("{1}>>>>>>>>>>" + cookieStore.getCookies());

        HttpGet loginGet1 = new HttpGet("http://10.170.200.171/");
        CloseableHttpResponse res2 = client.execute(loginGet1);

        System.out.println("{2}>>>>>>>>>>" + cookieStore.getCookies());

        HttpGet meGet = new HttpGet("http://10.170.200.171/crux-uc/api/user/me");
        CloseableHttpResponse res3 = client.execute(meGet);

        System.out.println(EntityUtils.toString(res3.getEntity()));
        System.out.println("{3}>>>>>>>>>>" + cookieStore.getCookies());

    }
}
